from django.shortcuts import render,redirect,reverse
from django.http import HttpResponse
from .models import Article
from django.views.generic import ListView,View
from django.utils.decorators import method_decorator
#给类视图添加装饰器  需要导入method_decorator

def login_required(func):
    def wrapper(request,*args,**kwargs):
        if request.GET.get("username"):
            return func(request,*args,**kwargs)
        else:
            # return redirect(reverse('front:login'))
            return redirect(reverse('errors:405'))
    return wrapper

def login(request):
    return HttpResponse("登陆")


# @method_decorator([login_required],name='dispatch')
class ProfileView(View):
    def get(self,request):
        return HttpResponse("个人中心")
    #所有的请求 get post patch put等 都肯定经过dispatch
    # @method_decorator(login_required)
    # def dispatch(self, request, *args, **kwargs):
    #     return super(ProfileView, self).dispatch(request, *args, **kwargs)
    def http_method_not_allowed(self, request, *args, **kwargs):
        # return HttpResponse("ASFADSF")
        return redirect(reverse('front:login'))
# Create your views here.
@login_required
def add_article(request):
    articles = []
    for x in range(0,110):
        article = Article(title='标题:%s' %x,content='内容:%s' %x)
        articles.append(article)
    Article.objects.bulk_create(articles)
    return HttpResponse("添加成功")


class ArticleView(ListView):
    model = Article
    template_name = 'article_list1.html'
    context_object_name = 'articles'
    paginate_by = 10
    ordering = 'create_time'
    page_kwarg = 'p'

    def get_context_data(self,**kwargs):
        context = super(ArticleView, self).get_context_data(**kwargs)
        paginator = context.get('paginator')
        page_obj = context.get('page_obj')
        pagination_data = self.get_paginate_data(paginator,page_obj,3)
        context.update(pagination_data) #更新字典的 内容
        # print(paginator.count)
        # print(paginator.num_pages)
        # print(paginator.page_range)
        # # print(page_obj.number)
        # # print(page_obj)


        return context
        #select * from user where id>1 limit 1,4;

    def get_paginate_data(self,paginator,page_obj,around=3):
        #总共多少页
        num_pages = paginator.num_pages
        #当前页
        current_page = page_obj.number

        left_has_more = False
        right_has_more = False
        #   4               123   4
        if current_page <= around+2:
            left_pages = range(1,current_page)

        else:                #678   9
            left_has_more = True
            left_pages = range(current_page-around,current_page)
            # 9     range(6,9)
        #19 20 21 22 23
        if current_page >= num_pages-around-1:
            right_pages = range(current_page+1,num_pages+1)

        else:
            #15 16 17 18
            right_has_more = True
            right_pages = range(current_page + 1,current_page+around+1)
        return {
            'left_pages':left_pages,
            'right_pages':right_pages,
            'current_page':current_page,
            'left_has_more':left_has_more,
            'right_has_more':right_has_more,
            'num_pages':num_pages

        }

    def get_queryset(self):
        return Article.objects.filter(id__lt=66)




